import { 
    queryUsers, 
    addUser ,
    updateUser,
    changeUserState, 
    getBranchTreeList, 
    changePassword,
    getPostListByBranchId,
    updatePassWord,
    existUserName
  } from '../services/api';
  import { message } from 'antd';
  import { isResponerCode } from '../utils/utils';
  export default {
    namespace: 'users',
  
    state: {
        modalVisible: false,
        userList: [],
        page:{},
        branchList: [],
        loading: false,
        detailValue: {},
        confirmLoading : false,
        jobsList: [],
    },
  
    effects: {
    
      *fetch({ payload ,callback}, { call, put }) {
        yield put({
          type: 'query',
          payload: {
            loading: true,
          },
        });
        const response = yield call(queryUsers, payload);
        
        if(response.result.code !== '200'){
          yield put({
            type: 'query',
            payload: {
              loading: false,
            },
          });
          message.error(response.result.message)
        }
        yield put({
          type: 'query',
          payload: {
            userList: response.data.list,
            page: response.data.page,
            loading: false,
          },
        });
        if(callback)callback();
      },
  
      *add({ payload, callback }, { call, put }) {
      
        yield put({
          type: 'changeSubmitting',
          payload: true,
        });
          const response = yield call(addUser, payload);
         
          if (!isResponerCode(response)) {
            return false
          }
          yield put({
            type: 'changeSubmitting',
            payload: false,
          });
  
          if(response.result.code=="200"){
            message.success('提交成功');
          }else{
            message.error(response.result.message);
          }
    
          yield put({
            type: 'save',
            payload: response,
          });
          if (callback) callback();
  
      },
  
      *update({ payload, callback }, { call, put }) {
       
        yield put({
          type: 'changeSubmitting',
          payload: true,
        });
          const response = yield call(updateUser, payload);
          
          if (!isResponerCode(response)) {
            return false
          }
          yield put({
            type: 'changeSubmitting',
            payload: false,
          });
  
          if(response.result.code=="200"){
            message.success('提交成功');
          }else{
            message.error(response.result.message);
          }
    
          yield put({
            type: 'save',
            payload: response,
          });
          if (callback) callback();
  
      },
  
      *changeUserState({ payload, callback }, { call, put }) {
  
        const response = yield call(changeUserState, payload);
       
        if (response.result.code == "200") {
          message.success('改变状态成功');
        } else {
          message.error(response.result.message);
        }
        yield put({
          type: 'save',
          payload: { data: response },
        });
        if (callback) callback();
  
      },
      //密码重置
      *changePassword({ payload, callback }, { call, put }) {
        const response = yield call(changePassword, payload);
        if (response.result.code == "200") {
          message.success('密码重置成功');
        } else {
          message.error(response.result.message);
        }
        yield put({
          type: 'save',
          payload: { data: response },
        });
        if (callback) callback();
      },
     
      *getPostListByBranchId({ payload }, { call, put }) {
       
        const response = yield call(getPostListByBranchId, payload);
       
        if (!isResponerCode(response)) {
          return false
        }
        yield put({
          type: 'query',
          payload: {
            jobsList: response.data,
          },      
        });
      },
  
      *updatePassWord({ payload, callback }, { call, put }) {
        const response = yield call(updatePassWord, payload);
        if(response.result.code == 200){
          message.success("密码修改成功")
          
        }else{
          message.error(response.result.message)
        }
        yield put({
          type: 'query',
          payload: {
            data: response.data,
          },      
        });
        if(callback)callback()
      },

      *changePassword({ payload, callback }, { call, put }) {
        const response = yield call(changePassword, payload);
        if(response.result.code == 200){
          message.success("密码重置成功")
          
        }else{
          message.error(response.result.message)
        }
        yield put({
          type: 'query',
          payload: {
            data: response.data,
          },      
        });
        if(callback)callback()
      },

      *existUserName({ payload, callback }, { call, put }) {   
        
        const response = yield call(existUserName, payload);
        
        if(response.result.code == 200){
        }else{
          message.error(response.result.message)
        }
        yield put({
          type: 'query',
          payload: {
            data: response.data,
          },      
        });
        if(callback)callback()
      },
  },
  
    reducers: {//纯函数， 输入什么输出就是什么
      query(state, action) {
        return {
          ...state,
          ...action.payload,
        };
      },
      showEdit (state, action) {
        return { 
          ...state,
          modalVisible: action.payload,
        };
      },
      setValue (state, action) {
        return { 
          ...state,
          detailValue: action.payload,
        };
      },
  
      save(state, action) {
        return {
          ...state,
          data: action.payload,
        };
      },
  
      changeSubmitting(state, { payload }) {
        return {
          ...state,
          confirmLoading: payload,
          modalVisible: !payload && state.modalVisible ?  false : state.modalVisible,
        };
      },
  
    },
  };
  